/** CS2340 Spring 2011 Project
 *  Team: 40 Jiggawatt Hard Driver
 *  Suk Hwan Hong
 *  Rhys Saraceni
 *  Travis Cooper
 *  Luke Stubb
 *
 * @author 40 Jiggawatt Hard Driver
 * @version 1.0 
 */

package edu.gatech.jiggawatt.office.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import edu.gatech.jiggawatt.office.presenter.Presenter;

/**
 * Represents Login screen
 * @author 40 Jiggawatt Hard Driver
 * @version 1.0
 *
 */
public class LoginFrame extends JFrame { // $codepro.audit.disable declareDefaultConstructors

	private JPanel contentPane;
    private JTextField idField;
    private JPasswordField pwField;
	private JLabel idLabel;
	private JLabel pwLabel;	
	private Presenter presenter;	
	private final int ADMIN_TYPE = 0, PATIENT_TYPE = 1, // $codepro.audit.disable instanceFieldNamingConvention, nonBlankFinalInstanceField
	NURSE_TYPE = 2, DOCTOR_TYPE = 3, ACCOUNT_LOCKED = 4; // $codepro.audit.disable instanceFieldNamingConvention, nonBlankFinalInstanceField, numericLiterals, unusedField

	/**
	 * Create the frame.
	 * @param p presenter
	 */
	public LoginFrame(Presenter p) 
	{
		setTitle("Awesome Office"); // $codepro.audit.disable stringLiterals
		presenter = p;
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300); // $codepro.audit.disable numericLiterals
		contentPane = new JPanel();
		contentPane.setLayout(null);
		setContentPane(contentPane);
		
		idField = new JTextField();
		idField.setBounds(178, 87, 86, 20); // $codepro.audit.disable numericLiterals
		idField.setColumns(10); // $codepro.audit.disable numericLiterals
		contentPane.add(idField);
		
		JButton btnLogin = new JButton("Login"); // $codepro.audit.disable stringLiterals
		
		//Login handler
		btnLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String id = idField.getText();
				String pw = new String(pwField.getPassword());
				
				//Empty case check
				if (0 == id.length()) {
					JOptionPane.showMessageDialog(null, "ID field is empty",  // $codepro.audit.disable stringLiterals
							"Error", JOptionPane.ERROR_MESSAGE); // $codepro.audit.disable stringLiterals
				}
				else if (0 == pw.length()) {
					JOptionPane.showMessageDialog(null, "PW field is empty",  // $codepro.audit.disable stringLiterals
							"Error", JOptionPane.ERROR_MESSAGE); // $codepro.audit.disable stringLiterals
				}
				//Ask presenter to check for login
				else {
					//-1 if login fails, 4 if account is locked
					//otherwise, loginSucess holds the userType
					int loginSuccess = presenter.login(id, pw);
					
					//Show appropriate screen according to the loginSuccess value
					if (loginSuccess == -1) {
						JOptionPane.showMessageDialog(null, "Login Attempt Failed",  // $codepro.audit.disable stringLiterals
								"Error", JOptionPane.ERROR_MESSAGE); // $codepro.audit.disable stringLiterals
					}
					else if (loginSuccess == ACCOUNT_LOCKED) {
						JOptionPane.showMessageDialog(null, 
								"Your Account is locked due to 3 consecutive miss",  // $codepro.audit.disable stringLiterals
								"Error", JOptionPane.ERROR_MESSAGE); // $codepro.audit.disable stringLiterals
					}
					else {
						if (loginSuccess == ADMIN_TYPE) {
							JFrame adminFrame = new AdminFrame(presenter, id);
							adminFrame.setVisible(true);
						}
						else if (loginSuccess == PATIENT_TYPE) {
							JFrame patientFrame = new PatientFrame(presenter, id);
							patientFrame.setVisible(true);
						}
						else if (loginSuccess == NURSE_TYPE) {
							JFrame nurseFrame = new NurseFrame(presenter, id);
							nurseFrame.setVisible(true);
						}
						else
						{
							JFrame doctorFrame = new DoctorFrame(presenter, id);
							doctorFrame.setVisible(true);
						}

						setVisible(false);
						dispose();
					}
				}
			}
		});
		
		btnLogin.setBounds(175, 149, 89, 23); // $codepro.audit.disable numericLiterals
		contentPane.add(btnLogin);
		
		JButton btnSave = new JButton("Save"); // $codepro.audit.disable stringLiterals
		btnSave.setBounds(175, 180, 89, 23); // $codepro.audit.disable numericLiterals
		contentPane.add(btnSave);
		
		JButton btnLoad = new JButton("Load"); // $codepro.audit.disable stringLiterals
		btnLoad.setBounds(175, 210, 89, 23); // $codepro.audit.disable numericLiterals
		contentPane.add(btnLoad);
		
		//Save button handler
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				JFileChooser chooser = new JFileChooser();
				int res = chooser.showDialog(null, "Save Database Data"); // $codepro.audit.disable stringLiterals
				if (res == JFileChooser.APPROVE_OPTION) {
				    presenter.save(chooser.getSelectedFile().getAbsolutePath());
				}
			}
		});
		
		//Load button handler
		btnLoad.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{				
					JFileChooser chooser = new JFileChooser();
					int res = chooser.showDialog(null, "Load Database data"); // $codepro.audit.disable stringLiterals
					if (res == JFileChooser.APPROVE_OPTION) {
					    presenter.load(chooser.getSelectedFile().getAbsolutePath());
					}
			}
				
		});
		
		idLabel = new JLabel("ID");
		idLabel.setBounds(145, 89, 16, 17); // $codepro.audit.disable numericLiterals
		contentPane.add(idLabel);
		
		pwLabel = new JLabel("PW");
		pwLabel.setBounds(145, 117, 23, 23); // $codepro.audit.disable numericLiterals
		contentPane.add(pwLabel);
		
		pwField = new JPasswordField();
		pwField.setBounds(178, 118, 86, 20); // $codepro.audit.disable numericLiterals
		contentPane.add(pwField);
	}
}
